package com.dxj1992.controller.admin;

import com.dxj1992.entity.AdminUsers;
import com.dxj1992.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class BaseController {

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    protected void startPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        // todo
//        PageHelper.startPage(pageNum, pageSize, "created_at desc").setReasonable(true);
        PageHelper.startPage(pageNum, pageSize).setReasonable(true);
    }

    /**
     * 获取用户缓存信息
     */
    public AdminUsers getSysUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        AdminUsers admin = (AdminUsers) session.getAttribute("admin");
        return admin;
    }


}
